import request from '@/utils/request'

// 流程定义相关接口
export const getProcessDefinitions = () => {
  return request.get('/process-definitions')
}

export const deployProcessDefinition = (formData) => {
  return request({
    url: '/process/process-definitions/deploy',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const deleteProcessDefinition = (id) => {
  return request.delete(`/process-definitions/${id}`)
}

// 流程实例相关接口
export const startProcess = (data) => {
  return request.post('/process/start', data)
}

export const getMyTasks = () => {
  return request.get('/process/tasks', {
    params: {
      userId: localStorage.getItem('username')
    }
  })
}

export const completeTask = (taskId, data) => {
  return request.post(`/process/tasks/${taskId}/complete`, data)
}

export const getMyRequests = () => {
  const userId = localStorage.getItem('userId')
  return request.get('/process/requests', {
    params: {
      userId: parseInt(userId)
    }
  })
}

// 更新流程定义
export const updateProcessDefinition = (id, formData) => {
  return request({
    url: `/process/process-definitions/${id}`,
    method: 'put',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取表单配置
export const getProcessFormConfig = (processDefinitionId) => {
  return request.get(`/process/process-definitions/${processDefinitionId}/form-config`)
}

// 保存表单配置
export const saveProcessFormConfig = (processDefinitionId, fields) => {
  return request.post(`/process/process-definitions/${processDefinitionId}/form-config`, fields)
}

// 获取用户列表
export const getUsers = () => {
  return request.get('/users')
}

// 获取审批历史
export const getProcessHistory = (processInstanceId) => {
  return request.get(`/process/process-instances/${processInstanceId}/history`)
}